<template>
  <div class="login">
      <div class="top-nav">
          <van-nav-bar title="欢迎回来！">
      <template #left>
        <div class="left-box">
          <div>
              LOGIN
          </div>
        </div>
      </template>
      
      <template #right>
        <div class="home-text" @click="goPage('Home')">首页</div>
      </template>
    </van-nav-bar>
      </div>

      
    <div class="content">
        <div class="logo">
            <img class="auto-img" src="../assets/images/logo.jpg" alt="" />
          </div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round type="info" native-type="submit" @click="oLog(username)">登录</van-button>
          <van-button round type="info" native-type="submit" @click="add">注册</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
    },
    goPage(name){
        this.$router.push({name});
    },
    add() {
        let userName = this.username;
        let passWord = this.password;
        let arr = [];

        function findArr() {
            for (let userName in localStorage) {
                arr.push(localStorage[userName]);
            }
            return arr;
        }
        let d = findArr();
        if (userName == d[1]) {
            alert('账户已注册');
            return false;
        } else {
            localStorage.userName = userName;
            localStorage.passWord = passWord;
            alert('注册成功');
        }
    },
    oLog(username) {
       let userName = this.username;
        let passWord = this.password;
        let arr = [];
        function findArr() {
            for (let userName in localStorage) {
                arr.push(localStorage[userName]);
            }
            return arr;
        }
        let d = findArr();
        if (userName == d[1] && passWord == d[0]) {
            this.$router.push({name:'My',params:{username}});
            //fn&&fn();
        } else if (userName == d[1] && passWord != d[0]) {
            alert('密码错误');
        } else if (userName != d[1]) {
            alert('用户不存在');
        }
    }
  },
};
</script>

<style lang="less" scoped>
.content {
  margin-top: 50px;
}
.van-button {
  // width: 300px;
  width: 100%;
  display: block;
  margin: 10px auto;
}
.logo{
    text-align: center;
    padding-bottom: 20px;
}
.auto-img{
    width: 64px;
    text-align: center;
}
/deep/.van-nav-bar__content{
    background-color: rgb(251, 67, 11);
    opacity: 0.8;
    color: white;
}
</style>